package com.zzyl.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlansDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingPlanProject;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingPlanProjectMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class NursingPlanServiceImpl implements NursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingPlanProjectMapper nursingPlanProjectMapper;
    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlanVo> plans=nursingPlanMapper.selectAll();
        return plans;
    }

    @Override
    public PageResponse<NursingPlanVo> pageQuery(String name, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);

        Page<NursingPlanVo> plans=nursingPlanMapper.selectByCondition(name,status);
        plans.stream().forEach(plan->plan.setCount(nursingPlanProjectMapper.selectByPlanId(plan.getId())));
        return PageResponse.of(plans, NursingPlanVo.class);
    }

    @Override
    public NursingPlanVo getById(Long id) {
        if (id == null||id==0) {
            throw new BaseException(BasicEnum.PLAN_ID_EMPTY);
        }
        NursingPlanVo nursingPlanVo=nursingPlanMapper.getById(id);
        return nursingPlanVo;

    }

    @Override
    public int addPlan(NursingPlanDto nursingPlanDto) {
        if (nursingPlanDto == null) {
            throw new BaseException(BasicEnum.PLAN_DTO_NULL);
        }
        NursingPlan nursingPlan=new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,nursingPlan);
        int row=nursingPlanMapper.addNPlan(nursingPlan);
        if (row>0) {
            Long id = nursingPlan.getId();
            List<NursingProjectPlansDto> projectPlansDto = nursingPlanDto.getProjectPlans();
            List<NursingPlanProject> list=new ArrayList<>();
            for (NursingProjectPlansDto plansDto : projectPlansDto) {
                NursingPlanProject nursingPlanProject=new NursingPlanProject();
                BeanUtils.copyProperties(plansDto,nursingPlanProject);
                nursingPlanProject.setPlanId(id);
                list.add(nursingPlanProject);
            }

           int rows= nursingPlanProjectMapper.insert(list);
            return rows;
        }
        throw new BaseException(BasicEnum.PLAN_ADD_FAIL);
    }

    @Override
    public int deleteById(Long id) {
        int total = nursingPlanProjectMapper.selectByPlanId(id);
        if (total != 0) {
            //如果关联项目计划，直接返回错误
            throw new BaseException(BasicEnum.PLAN_RELATED_PROJECT);

        }
        int row=nursingPlanMapper.deleteById(id);
        return row;
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        return nursingPlanMapper.updateStatus(id,status);
    }

    @Override
    public int update(NursingPlanDto nursingPlanDto) {
        if (nursingPlanDto==null){
            throw new BaseException(BasicEnum.PLAN_DTO_NULL);
        }
        NursingPlan plan=new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto,plan);
        plan.setId(nursingPlanDto.getId());
        int row=nursingPlanMapper.update(plan);
        List<NursingProjectPlansDto> projectPlansDto = nursingPlanDto.getProjectPlans();
        if (projectPlansDto==null||row<=0) {
            return row;
        }
        List<NursingPlanProject> list=new ArrayList<>();
        for (NursingProjectPlansDto plansDto : projectPlansDto) {
            nursingPlanProjectMapper.deleteByPlanId(nursingPlanDto.getId());
            NursingPlanProject pp=new NursingPlanProject();
            BeanUtils.copyProperties(plansDto,pp);
            pp.setPlanId(nursingPlanDto.getId());
            list.add(pp);
        }
        return nursingPlanProjectMapper.insert(list);
    }


}
